import { createConnection } from "typeorm";
import { Request, Response } from "express";
import { quests } from "../configs/configs";
import { Quest } from "../entity/Quest";

const moment = require('moment-timezone');
const connR = createConnection("role");

export function questRoute(app: any) {

    app.get('/api/quests', function (req: Request, res: Response) {

        res.send(quests);
    })

    app.get('/api/quest/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let data = await connection.getRepository(Quest).findOne(req.params.role_id);
            res.send(data)

        }).catch(error => console.log(error));
    })

    app.post('/api/quest/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            const repository = connection.getRepository(Quest);
            let quest = await repository.findOne(req.params.role_id);
            quest.quest_id = req.body.value;
            await repository.save(quest);

            res.send({ success: true })

        }).catch(error => res.send({ success: false, msg: error }));
    })
}
